$(function() {
    //layui分页对象
    var laypage = layui.laypage
        //layui弹层对象
    var layer = layui.layer
    var form = layui.form
    var userPage = {
        pagenum: 1,
        pagesize: 2,
        care_id: '',
        state: ''
    }

    //获取用户信息列表
    userList()

    function userList() {
        $.ajax({
            type: 'get',
            url: 'http://localhost:8888/admin/users',
            data: userPage,
            headers: {
                Authorization: localStorage.getItem('token')
            },
            success: function(res) {

                if (res.status !== 0) {
                    return $('tbody').html('暂无用户信息')
                }
                var htmlStr = template('tempData', res)
                    // console.log(htmlStr);
                $('tbody').html(htmlStr)
                    // console.log(res.total);
                count(res.total)
            }
        })
    }

    //分页函数
    function count(total) {
        //执行一个laypage实例
        laypage.render({
            elem: 'articlePage', //注意，这里的articlePage 是 ID，不用加 # 号
            count: total, //数据总数，从服务端得到,
            limit: userPage.pagesize,
            //起始位
            curr: userPage.pagenum,
            layout: ['prev', 'page', 'next'],
            jump: function(obj, first) {
                if (first === true) {
                    return
                } else {
                    userPage.pagesize = obj.limit
                    userPage.pagenum = obj.curr

                    userList()
                }
            }
        });
    }


    //删除
    $('tbody').on('click', '.delete', function() {
        var num = $('.delete').length
        console.log(num);
        //获取每条数据id
        var id = $(this).attr('data-id')
            //询问
        layer.confirm('确定删除这条数据吗?', { icon: 3, title: '提示' }, function(index) {
            $.ajax({
                type: 'delete',
                url: 'http://localhost:8888/admin/users/' + id,
                headers: {
                    Authorization: localStorage.getItem('token')
                },
                success: function(res) {
                    if (res.status !== 0) {
                        return layer.msg('删除失败')
                    }
                    layer.msg('删除数据成功', function() {
                        if (num == 1) {
                            userPage.pagenum = userPage.pagenum === 1 ? 1 : userPage.pagenum - 1
                        }
                        if (num == 0) {
                            userPage.pagenum = 1
                        }
                        userList()
                    })

                }
            })
            layer.close(index);
        });
    })

    //根据id获取用户信息
    $('tbody').on('click', '.bj', function() {
        var id = $(this).attr('data-id')
        window.parent.id = id

    })
    form.verify({
            pass: [
                /^[\S]{6,12}$/, '密码必须6到12位，且不能出现空格'
            ],
            rePwd: function(value) {
                var newnew = $('[name=password]').val().trim()
                if (value !== newnew) {
                    return '两次密码输入不一致'
                }
            }
        })
        //修改密码
        //点击动态生成的按钮弹出模态款
    $('tbody').on('click', '.resetpwd', function() {
        var id = $(this).attr('data-id')
        var index = layer.open({
            type: 1,
            content: $('#repwd-form-tpl').html(),
            title: '重置密码',
            area: ['500px', '300px']
        });

        //给动态生成的模态框绑定submit事件
        $('body').on('submit', '#repwd-form', function(e) {
            e.preventDefault()
            console.log(id);
            var data = $(this).serialize()
            $.ajax({
                type: 'PUT',
                url: 'http://localhost:8888/admin/users/' + id,
                headers: {
                    Authorization: localStorage.getItem('token')
                },
                data: data,
                success: function(res) {
                    if (res.status !== 0) {
                        return layer.msg('重置密码失败')
                    }
                    layer.msg('重置密码成功', function() {
                        location.href = '/user/user.html'
                    })
                }
            })

        })
    })



})